/*
* 统一处理要识别的md文件
* */
import mdRouterList from './mdRouter'


//解析md
class CreateMDRouter {
    key:number = 0
    history:any = [
        // {
        //     name:'test1',
        //     key:0,
        //     open:true,
        // },
        // {
        //     name:'test',
        //     key:1,
        //     open:false,
        // }
    ]
    //打开md文件
    push = (arry:any) =>{
        return new Promise((resolve, reject) => {
            if (arry.name){
                if (this.key>0&&this.history[this.history.length - 1].name == arry.name){
                    //数组最后一个历史是当前（open为true）
                    if (this.history[this.history.length - 1].open){
                        resolve('[MDR]已经是当前md文件了，无需前往')
                    }else {
                        //这种情况属于 当前页不是最后一个，但是点击前往最后一个，此时不再记录进历史栈
                        let obj:any = mdRouterList.find((item:any )=> {
                            return (item.name == arry.name)
                        })
                        this.alterHis(this.history[this.history.length - 1])
                        // 解析md文件
                        obj.component().then((res:any)=>{
                            resolve({...res})
                        })
                    }

                } else {
                    // 如果最后一个open是false，那新来的就替换掉最后一个

                    let obj:any = mdRouterList.find((item:any )=> {
                        return (item.name == arry.name)
                    })

                    if (obj){
                        if (this.history.length>1&&!this.history[this.history.length - 1].open){
                            this.history[this.history.length - 1].name = obj.name
                            this.alterHis(this.history[this.history.length - 1])
                        }else {
                            //改变key 记录进历史栈
                            this.changeKey()
                            this.history.push({name:obj.name,key:this.key,open:true})
                        }
                        this.alterHis(this.history[this.history.length - 1])
                        // 解析md文件
                        obj.component().then((res:any)=>{
                            resolve({...res})
                        })


                    }else {
                        reject('[MDR]没有找到匹配的md文件!')
                    }


                }
            }else {
                reject('[MDR]请确保参数正确：{name:"name"}')
            }
        })
    }
    //跳转历史栈
    go = (num:number)=>{
        return new Promise((resolve, reject) => {
            //根据open先找到当前的路由 拿到key 就知道在历史栈的位置
            let his:any = this.history.find((o:any)=> o.open)
            if (his){
                //拿到了路由 确认它的位置 因为history的key是从1开始的，所以和数组下标相差1
                let index:number = (his.key + num) - 1

                if ( index < 0||index > this.history.length){
                    reject('[MDR]路径错误')
                }else {
                    //去改变历史栈
                    this.alterHis(this.history[index])
                    let obj:any = mdRouterList.find((item:any )=> {
                        return (item.name === this.history[index].name)
                    })
                    this.key = this.history[index].key
                    // 解析md文件
                    obj.component().then((res:any)=>{
                        resolve({...res,name:this.history[index].name})
                    })

                }
            }else {
                reject('[MDR]路径不存在')
            }
        })

    }
    //
    changeKey(){
        if (this.history.length>1){
            let endKey = this.history[this.history.length - 1].key
            // console.log('endKey',endKey)
            if (endKey!=this.key){
                this.key = endKey+1
            }else {
                this.key+=1
            }
        }else {
            this.key+=1
        }


    }

    //改变历史栈
    alterHis = (obj:any)=>{
        this.history.forEach((item:any)=>{
            item.open = item.key === obj.key;
        })
    }

}

export default CreateMDRouter